//
//  KZLetterAnalysisView.m
//  听说弱点强化训练
//
//  Created by 朱凯 on 2018/8/27.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "KZLetterAnalysisView.h"

@implementation KZLetterAnalysisView
{
    UIScrollView *_scroll;
}

- (instancetype)init {
    
    if (self = [super init]) {
        
        _scroll = [UIScrollView new];
        [self addSubview:_scroll];
        [_scroll mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(@20);
            make.left.equalTo(@20);
            make.right.equalTo(@-20);
            make.bottom.equalTo(@-10);
        }];
    }
    return self;
}

- (void)setupUI {
    
    [_scroll.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

    // 标题
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"解析课件";
    titleLabel.font = FONT(15);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor blackColor];
    [_scroll addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(@0);
        make.top.equalTo(@0);
    }];
    
    // 音标
    UILabel *pronounceLabel = [[UILabel alloc] init];
    pronounceLabel.text = @"英[abc]";
    pronounceLabel.font = FONT(12);
    pronounceLabel.textAlignment = NSTextAlignmentLeft;
    pronounceLabel.textColor = [UIColor colorWithHex:0x1C1C1C];
    [_scroll addSubview:pronounceLabel];
    [pronounceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.top.equalTo(titleLabel.mas_bottom).offset(10);
    }];
    
    // 翻译
    UILabel *translationLabel = [[UILabel alloc] init];
    translationLabel.numberOfLines = 0;
    translationLabel.text = @"n. 向木媛 缘佛手钱";
    translationLabel.font = FONT(12);
    translationLabel.textAlignment = NSTextAlignmentLeft;
    translationLabel.textColor = [UIColor colorWithHex:0x1C1C1C];
    [_scroll addSubview:translationLabel];
    [translationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.top.equalTo(pronounceLabel.mas_bottom).offset(10);
    }];
    
    // 例句
    UIView *last = nil;
    for (int i = 0; i < 3; i ++) {
        
        UILabel *sentenceLabel = [[UILabel alloc] init];
        sentenceLabel.numberOfLines = 0;
        sentenceLabel.text = @"I am a boy";
        sentenceLabel.font = FONT(12);
        sentenceLabel.textAlignment = NSTextAlignmentLeft;
        sentenceLabel.textColor = [UIColor colorWithHex:0x1C1C1C];
        [_scroll addSubview:sentenceLabel];
        [sentenceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(@0);
            if (last) {
                make.top.equalTo(last.mas_bottom).offset(15);
            } else {
                make.top.equalTo(translationLabel.mas_bottom).offset(15);
            }
        }];
        
        // 翻译
        UILabel *sentenceTranslationLabel = [[UILabel alloc] init];
        sentenceTranslationLabel.numberOfLines = 0;
        sentenceTranslationLabel.text = @"我是个男孩";
        sentenceTranslationLabel.font = FONT(12);
        sentenceTranslationLabel.textAlignment = NSTextAlignmentLeft;
        sentenceTranslationLabel.textColor = [UIColor colorWithHex:0x1C1C1C];
        [_scroll addSubview:sentenceTranslationLabel];
        [sentenceTranslationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(@0);
            make.top.equalTo(sentenceLabel.mas_bottom).offset(3);
        }];
        
        last = sentenceTranslationLabel;
    }
    
    
    [last mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@0);
    }];
}

- (void)setModel:(KZVocabularyCurrentPackageDetailModel *)model {
    
    _model = model;
    
    [self setupUI];
}



#pragma mark -
- (void)showWithPoint:(CGPoint)point rect:(CGRect)rect view:(UIView *)view {
    
    [view addSubview:self];
    self.frame = rect;
    
    CGFloat width = rect.size.width;
    CGFloat height = rect.size.height;
    CGPoint p = [view convertPoint:point toView:self];
    
    UIBezierPath *borderPath = [UIBezierPath bezierPath];
    CGFloat corner = 8;
    CGFloat arrowSide = 5;
    CGFloat x = p.x;
    CGFloat y = p.y;
    // 顺时针
    [borderPath moveToPoint:CGPointMake(x, y)];
    x = p.x + arrowSide;
    y = p.y + arrowSide;
    [borderPath addLineToPoint:CGPointMake(x, y)];
    x = width - corner;
    y = y;
    [borderPath addLineToPoint:CGPointMake(x, y)];
    // 右上圆角
    x = x;
    y = y + corner;
    [borderPath addArcWithCenter:CGPointMake(x, y) radius:corner startAngle:-M_PI/2 endAngle:0 clockwise:true];
    x = width;
    y = y;
    [borderPath addLineToPoint:CGPointMake(x, y)];
    x = width;
    y = height - corner;
    [borderPath addLineToPoint:CGPointMake(x, y)];
    // 右下圆角
    x = x - corner;
    y = y;
    [borderPath addArcWithCenter:CGPointMake(x, y) radius:corner startAngle:0 endAngle:M_PI/2 clockwise:true];
    x = x;
    y = height;
    [borderPath addLineToPoint:CGPointMake(x, y)];
    x = corner;
    y = y;
    [borderPath addLineToPoint:CGPointMake(x, y)];
    // 左下圆角
    x = x;
    y = height - corner;
    [borderPath addArcWithCenter:CGPointMake(x, y) radius:corner startAngle:M_PI/2 endAngle:M_PI clockwise:true];
    x = 0;
    y = y;
    [borderPath addLineToPoint:CGPointMake(x, y)];
    x = x;
    y = p.y + arrowSide + corner;
    [borderPath addLineToPoint:CGPointMake(x, y)];
    // 左上圆角
    x = corner;
    y = y;
    [borderPath addArcWithCenter:CGPointMake(x, y) radius:corner startAngle:M_PI endAngle:M_PI*1.5 clockwise:true];
    x = x;
    y = y - corner;
    [borderPath addLineToPoint:CGPointMake(x, y)];
    x = p.x - arrowSide;
    y = y;
    [borderPath addLineToPoint:CGPointMake(x, y)];
    
    [borderPath closePath];
    
    CAShapeLayer *borderLayer = [CAShapeLayer layer];
    borderLayer.path = borderPath.CGPath;
    borderLayer.fillColor = [UIColor.whiteColor colorWithAlphaComponent:1].CGColor;
    borderLayer.lineWidth = 2;
    borderLayer.strokeColor = KZThemeColor2.CGColor;

    
    [self.layer insertSublayer:borderLayer atIndex:0];
}

@end
